class Page < ActiveRecord::Base
  has_many(:folder_content, :dependent => :destroy)
  has_many(:folder, :through => :folder_content)

  validates_presence_of(:title)

  has_and_belongs_to_many(:document_category, :order => "title")
  has_and_belongs_to_many(:user_group)
  has_and_belongs_to_many(:editor_group)

  class << self
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Spell check 
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def spell_check(text, user_id)
      # shell out to aspell and retrieve the results
      aspell_program = 'aspell' # every other OS on the planet
      temp_file = "#{RAILS_ROOT}/tmp/spell_#{user_id}_#{DateTime.now}.txt"
      File.open(temp_file, "wb") { |f| f.write(text) }
      command_line = "\"#{aspell_program}\" -a --lang=en_US --encoding=utf-8 -H < #{temp_file}"
      output = IO.popen(command_line) { |f| f.read }
      lines = output.split("\n")
        
      # build an array of stuff to put in the javascript
      words = []
      lines.each do |line|
        if line[0,1] == '&'
          details = line.split(' ', 5)
          words << [escape(details[1]), get_suggestions(details[4])]
        end
      end
      File.delete(temp_file)    
      words
    end
        
    def get_suggestions(value)
      value.split(', ').collect {|x| "#{escape(x)}"}.join(', ')
    end
        
    # properly escape single-quote for javascript
    def escape(x)
      x.gsub(/'/, '\\\\\'')
    end
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #end spell check 
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    def list_by_folder
      rvalue = [] 
      find(:all).each do |pg|
        rvalue << {:name => pg.qualified_name, :id => pg.id}
      end
      rvalue.sort{|x,y| x[:name] <=> y[:name]}
    end

    def search_for_values(values)
      extended_find(values)
    end

    private
      def extended_find(values)
        econditions = extended_conditions(values)
        restraints = ""
        restraints = " where #{econditions}" if econditions.length > 0
        results = find_by_sql("select p.* from pages p #{restraints}")
        rvalue = []
        results.each do |pg|
          rvalue << {:name => pg.qualified_name, :id => pg.id}
        end
        rvalue.sort{|x,y| x[:name] <=> y[:name]}
      end

      def extended_conditions(values)
        fields = %w(p.title p.content)
        conditions = Utils::Search.or_search_conditions(fields,values)
        return "( #{conditions} )" if conditions.length > 0
        return ""
      end

  end # end class block


  def show?
    !(do_hide || !enabled)
  end

  def menu_show?
    enabled && !show_in_footer && !do_hide
  end

  def current_folder_id
    return folder[0].id if folder.length > 0
    -1
  end

  def current_folder
    return folder[0] if folder.length > 0
    nil
  end

  def qualified_name
    return folder[0].qualified_name + " : " + title if folder.length > 0
    title
  end
end
